package com.lxk.config;

import com.lxk.aop.LoginInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author JohnSoybean
 * @create 2020/6/9 19:00
 * @description
 */
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

    /**
     * 配置所有请求都允许跨域
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("*")
                .allowedHeaders("*");
    }


    /**
     * 轻量级登录拦截
     * @return
     */
    @Bean
    public LoginInterceptor setLoginBean(){
        return new LoginInterceptor();
    }

    /**
     * 配置拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(setLoginBean())
                .addPathPatterns("/**")
                .excludePathPatterns(
                        "/screen/**",   //数据大屏显示
                        "/qrcode/**",   //静态资源
                        "/mini/**", "/web/**", "/aka/**",//登录
                        "/wxpay/afterpay",   //微信支付回调
                        "/activity/**", "/product/**", "/upload/**"
                        );
    }




//    /**
//     * 手动配置资源文件夹
//     * @param registry
//     */
//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        registry.addResourceHandler("/api/file/**").addResourceLocations("file:" + Constants.IMG_URL);
//    }


}
